<section class="content-header">
    <h1>
        系统管理
        <small>模块管理</small>
    </h1>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <!-- /.col -->
        <div class="col-md-12">
            <div class="box">
                <!--<div class="box-header with-border">-->
                    <!--<h3 class="box-title">配置管理</h3>-->
                    <!--&lt;!&ndash; /.box-tools &ndash;&gt;-->
                <!--</div>-->
                <!-- /.box-header -->
                <div class="box-body no-padding">
                    <div class="mailbox-controls">

                        <div class="navbar navbar-default">
                            <form id="search-form" class="navbar-form form-inline" onsubmit="return false">                                &nbsp;
                                <div class="form-group">
                                    <label>搜索条件</label>
                                    <input type="text" name="id" value="" placeholder="搜索ID"
                                           id="input-id" class="form-control" style="width: 200px;">
                                </div>

                                <div class="form-group">
                                    <input type="text" name="keyword" value="" placeholder="搜索模块名称，别名"
                                           id="input-keyword" class="form-control" style="width: 300px;">
                                </div>

                                <div class="form-group">
                                    <label>已安装</label>
                                    <select name="isSetup" id="input-isSetup" class="form-control">
                                        <option value="">所有</option>
                                        {{#each setupStatus}}
                                        <option value="{{key}}">{{title}}</option>
                                        {{/each}}
                                    </select>
                                </div>

                                <div class="btn-group">
                                    <button type="button" onclick="get_page(1)"
                                            class="btn btn-primary"><i
                                            class="fa fa-search"></i> 筛选
                                    </button>

                                    <button type="button" class="btn btn-danger"
                                            onclick="batchOperation(uninstallModule)">卸载
                                    </button>
                                    <button type="button" class="btn btn-success"
                                            onclick="batchOperation(installModule)">安装
                                    </button>

                                    <div class="btn-group">
                                        <a href="/admin/module/reset" class="btn btn-danger" title="模块数据不一致时，初始化模块信息，删除不存在的模块记录。">
                                            重置数据
                                        </a>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <!-- /.btn-group -->
                    </div>
                    <div id="records-table"></div>
                    <!-- /.mail-box-messages -->
                </div>
            </div>
            <!-- /. box -->
        </div>
        <!-- /.col -->
    </div>
</section>

<script>
    let curr_page=1;

    //分页加载函数
    function get_page(page){
        curr_page =page;
        const index = layer.load(0, {time: 5 * 1000}); //显示加载中动画，最多5秒自动关闭
        $.ajax({
            type: "post",
            url: `/admin/module/table?page=${page}`,
            data: {
                keywords:$('#input-keyword').val(),
                isSetup:$('#input-isSetup').val(),
                id:$('#input-id').val()
            },
            beforeSend: function (xhr) {
                // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.setRequestHeader('x-pjax', 'true');
            },
            success: function (data) {
                if (data.hasOwnProperty('code')){
                    layer.msg(data.data, {icon: 2});
                }else{
                    $('#records-table').html(data);
                }

                layer.close(index); //关闭加载动画
            },
            error: function (xhr) {
                layer.close(index); //关闭加载动画
                layer.msg('通讯失败！请重试！', {icon: 2});
            }
        });
    }

    function uninstallModule(id){
        layer.confirm('确认卸载模块吗？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                type: "post",
                url: `/admin/module/uninstall`,
                data: {id:id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('x-pjax', 'false');
                },
                success: function (data) {
                    // $(obj).parent().parent().remove();
                    layer.msg('模块卸载成功', {icon: 1});
                    // console.log(data);
                    // get_page(curr_page);
                    window.location.href="/admin/module/index";
                },
                error: function (xhr) {
                    layer.msg('通讯失败！请重试！', {icon: 2});
                }
            });
        }, function(){
            layer.msg('你放弃了操作', {icon: 2});
        });
    }

    function installModule(id){
        layer.confirm('确认安装模块吗？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                type: "post",
                url: `/admin/module/install`,
                data: {id:id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('x-pjax', 'false');
                },
                success: function (data) {
                    // $(obj).parent().parent().remove();
                    layer.msg('模块安装成功', {icon: 1});
                    // console.log(data);
                    // get_page(curr_page);
                    window.location.href="/admin/module/index";
                },
                error: function (xhr) {
                    layer.msg('通讯失败！请重试！', {icon: 2});
                }
            });
        }, function(){
            layer.msg('你放弃了操作', {icon: 2});
        });
    }

    function deleteModule(id){
        layer.confirm('确认删除模块数据吗？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                type: "post",
                url: `/admin/module/delete`,
                data: {id:id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('x-pjax', 'false');
                },
                success: function (data) {
                    // $(obj).parent().parent().remove();
                    layer.msg('模块删除成功', {icon: 1});
                    // console.log(data);
                    // get_page(curr_page);
                    window.location.href="/admin/module/index";
                },
                error: function (xhr) {
                    layer.msg('通讯失败！请重试！', {icon: 2});
                }
            });
        }, function(){
            layer.msg('你放弃了操作', {icon: 2});
        });
    }

    $( document ).ready(function() {
        get_page(1);
    });
</script>